﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using iTextSharp.text;
using iTextSharp.text.pdf;
using IPSWWA.Models;

namespace IPSWWA.Models
{
    public interface IPDFforms
    {
        //Used to fill in formfields
        void Stampfile(WorkingObject working);
    }

    public interface IPDFreturninmemory
    {
		//Prefer to return the pdf in memory as opposed to write to disk
        MemoryStream Returnfileinmemory();
    }

    public abstract class PDFforms : IPDFforms
    {
		//a lot of these classes are from the itextsharp library
        protected PdfReader reader;
        protected PdfStamper stamper;

        public abstract void Stampfile(WorkingObject working);
    }

    public abstract class PDFfaxforms : PDFforms , IPDFreturninmemory
    {
        protected MemoryStream input;
        protected MemoryStream output;

        public MemoryStream Returnfileinmemory()
        {
            return output;
        }

        public void Closetooutput()
        {
            stamper.Close();
            reader.Close();
            input.Flush();
            output = new MemoryStream(input.ToArray());
            input.Close();
        }

        public void Loadvalues(string pdffile, MemoryStream memory)
        {
            input = memory;
            reader = new PdfReader(pdffile);
            stamper = new PdfStamper(reader, input);
        }

        public void Loadvalues(string pdffile)
        {
            input = new MemoryStream();
            reader = new PdfReader(pdffile);
            stamper = new PdfStamper(reader, input);
        }
    }

    public class Genentechform : PDFfaxforms
    {
        public Genentechform()
        {
        }

        public Genentechform(string pdffile)
        {
            input = new MemoryStream();
            reader = new PdfReader(pdffile);
            stamper = new PdfStamper(reader, input);
        }

        public Genentechform(MemoryStream memory, string pdffile)
        {
            input = memory;
            reader = new PdfReader(pdffile);
            stamper = new PdfStamper(reader, input);
        }

        public override void Stampfile(WorkingObject working)
        {
            //need to add stamping actions to this
			//Removed a lot of fields here to show code.
            stamper.AcroFields.SetField("DR NAME", working.Doctor.Drname);
            stamper.AcroFields.SetField("PT NAME", working.Patient.Ptname);
            this.Closetooutput();
        }
    }

    public class Merckform : PDFfaxforms
    {
        public Merckform()
        {
        }

        public Merckform(string pdffile)
        {
            input = new MemoryStream();
            reader = new PdfReader(pdffile);
            stamper = new PdfStamper(reader, input);
        }

        public Merckform(MemoryStream memory, string pdffile)
        {
            input = memory;
            reader = new PdfReader(pdffile);
            stamper = new PdfStamper(reader, input);
        }

        public override void Stampfile(WorkingObject working)
        {
            //need to add stamping actions to this to fill in rest of forms
			//Removed a lot of fields here to show code.
            stamper.AcroFields.SetField("DR NAME", working.Doctor.Drname);
            stamper.AcroFields.SetField("PT NAME", working.Patient.Ptname);
            this.Closetooutput();
        }
    }

    public class Abbottform : PDFfaxforms
    {
        public Abbottform()
        {
        }

        public Abbottform(string pdffile)
        {
            input = new MemoryStream();
            reader = new PdfReader(pdffile);
            stamper = new PdfStamper(reader, input);
        }

        public Abbottform(MemoryStream memory, string pdffile)
        {
            input = memory;
            reader = new PdfReader(pdffile);
            stamper = new PdfStamper(reader, input);
        }

        public override void Stampfile(WorkingObject working)
        {
            //need to add stamping actions to this
			//Removed a lot of fields here to show code.
            stamper.AcroFields.SetField("DR NAME", working.Doctor.Drname);
            stamper.AcroFields.SetField("PT NAME", working.Patient.Ptname);
            this.Closetooutput();
        }
    }
}